package com.wing.order.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.order.model.entity.OrderReceiveLoad;
import com.wing.order.model.request.OrderReceiveLoadForm;
import com.wing.order.model.response.OrderReceiveLoadDetailVO;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.order.model.entity.OrderReceiveLoadDetail;
import com.wing.order.model.request.OrderReceiveLoadDetailForm;
import com.wing.order.service.OrderReceiveLoadDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-下货记录管理", tags = "#/order/orderReceiveLoadDetail")
@RestController("orderReceiveLoadDetailController")
@RequestMapping("order/admin/orderReceiveLoadDetail")
@RequiredArgsConstructor
public class OrderReceiveLoadDetailController {

    private final OrderReceiveLoadDetailService orderReceiveLoadDetailService;


    @ApiOperation(value = "查询所有下货记录", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<OrderReceiveLoadDetailVO> list(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "desc") String order
            , Long loadId
            , String code
            , String startTime
            , String endTime
            , Long departmentId
            , Long creatorId
            , String cabCode
            , String spaceCode
            , String skuCode
            , OrderReceiveLoadForm.LoadType loadType
            ,String skuCode4Query
            ,Long id
            ){

        Page<OrderReceiveLoadDetail> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", StrUtil.toUnderlineCase(sort));
            params.put("order", order);
        }
        if(loadId != null){
            params.put("loadId",loadId);
        }
       if(StringUtils.isNotBlank(code)){
           params.put("code",code);
       }
        if(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)){
            params.put("startCreateTime",startTime);
            params.put("endCreateTime",endTime);
        }
        //搜索
        if(departmentId != null){
            params.put("departmentId",departmentId);
        }
        if(creatorId != null){
            params.put("creator",creatorId);
        }
        if(StringUtils.isNotBlank(cabCode)){
            params.put("cabCode",cabCode);
        }
        if(StringUtils.isNotBlank(spaceCode)){
            params.put("spaceCode",spaceCode);
        }
        if(StringUtils.isNotBlank(skuCode4Query)){
            params.put("skuCode",skuCode4Query);
        }
        if(StringUtils.isNotBlank(skuCode)){
            params.put("skuCode",skuCode);
        }
        if(id != null){
            params.put("id",id);
        }
        if (loadType!=null){
            if (OrderReceiveLoadForm.LoadType.load.equals(loadType)){
                params.put("types",new Integer[]{OrderReceiveLoad.Type.selfLoad.getValue(),OrderReceiveLoad.Type.selfLoadSimple.getValue(),OrderReceiveLoad.Type.selfBatchLoadSimple.getValue()});
            }else{
                params.put("types",new Integer[]{OrderReceiveLoad.Type.selfUnloadSimple.getValue(),OrderReceiveLoad.Type.selfBatchUnloadSimple.getValue()});
            }

        }
        IPage<OrderReceiveLoadDetailVO> list = orderReceiveLoadDetailService.pageList4app(pg,params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "添加上货明细", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(OrderReceiveLoadDetailForm.Add.class) OrderReceiveLoadDetailForm orderReceiveLoadDetailForm) {
        if (orderReceiveLoadDetailService.add(orderReceiveLoadDetailForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }




    @ApiOperation(value = "修改上货明细", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(OrderReceiveLoadDetailForm.Update.class) OrderReceiveLoadDetailForm orderReceiveLoadDetailForm) {
        OrderReceiveLoadDetail orderReceiveLoadDetail = orderReceiveLoadDetailService.getById(orderReceiveLoadDetailForm.getId());
        if(orderReceiveLoadDetail == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(OrderReceiveLoadDetail.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(orderReceiveLoadDetailForm, orderReceiveLoadDetail, copyOptions);
        if (orderReceiveLoadDetailService.updateById(orderReceiveLoadDetail)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }



    @ApiOperation(value = "删除上货明细", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderReceiveLoadDetailId", value = "上货明细id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long orderReceiveLoadDetailId) {
        if (orderReceiveLoadDetailService.removeById(orderReceiveLoadDetailId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }
}
